package com.hzjy.api.service.impl;

import com.hzjy.api.beans.HzjyAcceptAddress;
import com.hzjy.api.mapper.HzjyAcceptAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 用户收货地址Service业务层处理
 * 
 * @author hzjy
 * @date 2020-08-27
 */
@Service
public class HzjyAcceptAddressServiceImpl
{
    @Autowired
    private HzjyAcceptAddressMapper hzjyAcceptAddressMapper;

    /**
     * 查询用户收货地址
     * 
     * @param acceptAddressId 用户收货地址ID
     * @return 用户收货地址
     */
    public HzjyAcceptAddress selectHzjyAcceptAddressById(Long acceptAddressId)
    {
        return hzjyAcceptAddressMapper.selectHzjyAcceptAddressById(acceptAddressId);
    }

    /**
     * 查询用户收货地址列表
     * 
     * @param hzjyAcceptAddress 用户收货地址
     * @return 用户收货地址
     */
    public List<HzjyAcceptAddress> selectHzjyAcceptAddressList(HzjyAcceptAddress hzjyAcceptAddress)
    {
        return hzjyAcceptAddressMapper.selectHzjyAcceptAddressList(hzjyAcceptAddress);
    }

    /**
     * 新增用户收货地址
     * 
     * @param hzjyAcceptAddress 用户收货地址
     * @return 结果
     */
    public int insertHzjyAcceptAddress(HzjyAcceptAddress hzjyAcceptAddress)
    {
        if (Integer.parseInt(hzjyAcceptAddress.getState().toString()) == 3){
            //如果设为默认,查询用户是否有默认地址
            HzjyAcceptAddress hzjyDefaultAcceptAddress = hzjyAcceptAddressMapper.selectDefaultAcceptAddress(hzjyAcceptAddress.getUserId());
            if (hzjyDefaultAcceptAddress != null){
                //如果有默认地址，则设为非默认
                hzjyDefaultAcceptAddress.setState(2L);
                hzjyAcceptAddressMapper.updateHzjyAcceptAddress(hzjyDefaultAcceptAddress);
            }
        }
        return hzjyAcceptAddressMapper.insertHzjyAcceptAddress(hzjyAcceptAddress);
    }

    /**
     * 修改用户收货地址
     * 
     * @param hzjyAcceptAddress 用户收货地址
     * @return 结果
     */
    public int updateHzjyAcceptAddress(HzjyAcceptAddress hzjyAcceptAddress)
    {
        if (Integer.parseInt(hzjyAcceptAddress.getState().toString()) == 3){
            //如果设为默认,查询用户是否有默认地址
            HzjyAcceptAddress hzjyDefaultAcceptAddress = hzjyAcceptAddressMapper.selectDefaultAcceptAddress(hzjyAcceptAddress.getUserId());
            if (!hzjyDefaultAcceptAddress.getUserId().equals(hzjyAcceptAddress.getUserId())){
                //如果查出来的默认地址id不是当前修改地址的id
                hzjyDefaultAcceptAddress.setState(2L);
                hzjyAcceptAddressMapper.updateHzjyAcceptAddress(hzjyDefaultAcceptAddress);
            }
        }
        return hzjyAcceptAddressMapper.updateHzjyAcceptAddress(hzjyAcceptAddress);
    }

    /**
     * 批量删除用户收货地址
     * 
     * @param acceptAddressIds 需要删除的用户收货地址ID
     * @return 结果
     */
    public int deleteHzjyAcceptAddressByIds(Long[] acceptAddressIds)
    {
        return hzjyAcceptAddressMapper.deleteHzjyAcceptAddressByIds(acceptAddressIds);
    }

    /**
     * 删除用户收货地址信息
     * 
     * @param acceptAddressId 用户收货地址ID
     * @return 结果
     */
    public int deleteHzjyAcceptAddressById(Long acceptAddressId)
    {
        return hzjyAcceptAddressMapper.deleteHzjyAcceptAddressById(acceptAddressId);
    }

    /**
     * 设置默认收货地址
     * @param hzjyAcceptAddress
     */
    public void defaultAcceptAddress(HzjyAcceptAddress hzjyAcceptAddress) {

        //将原有默认地址设为非默认
        HzjyAcceptAddress hzjyDefaultAcceptAddress = hzjyAcceptAddressMapper.selectDefaultAcceptAddress(hzjyAcceptAddress.getUserId());
        if (hzjyAcceptAddress.getState().equals(3L)){
            //如果是设为默认地址
            if (hzjyDefaultAcceptAddress != null){
                //将原有默认地址设为非默认
                hzjyDefaultAcceptAddress.setState(2L);
                hzjyAcceptAddressMapper.updateHzjyAcceptAddress(hzjyDefaultAcceptAddress);
            }
        }

        hzjyAcceptAddressMapper.updateHzjyAcceptAddress(hzjyAcceptAddress);
    }
}
